package BasicEngine
{
	import flash.events.ErrorEvent;
	import flash.utils.Dictionary;
	//实体管理器
	//实体的树形存储存储结构
	public class EntityManager
	{
		//实体索引字典
		public static var EntityIndexes:Dictionary;
		
		//根实体ID
		public static var RootId:int = 0;
		//根实体
		private static var RootEntity:Entity;
		
		//实体递增ID
		private static var addEntId:int = 100;
		
		//构造函数
		public function EntityManager()
		{
			
		}
		
		//初始化
		public static function Init():void
		{
			EntityIndexes = new Dictionary();
			CreateRoot();
		}
		
		//创建并返回根实体
		public static function CreateRoot():Entity
		{
			var rootEnt:Entity = new Entity();
			rootEnt.ID = RootId;
			rootEnt.Name = "根实体";
			rootEnt.Type = 0;
			RootEntity = rootEnt;
			EntityIndexes[RootId] = RootEntity;
			return rootEnt;
		}
		
		//创建实体
		public static function CreateEntityWithID(_parent:int, _name:String, _type:int=1):int
		{
			var parentEnt:Entity = GetEntityByID(_parent);
			return (CreateEntity(parentEnt, _name, _type));			
		}
		
		//创建实体
		public static function CreateEntity(_parent:Entity, _name:String, _type:int):int
		{
			if (_parent == null)
			    return -1;
			var newEnt:Entity = new Entity();
			newEnt.ID = addEntId++;
			newEnt.Name = _name;
			EntityIndexes[newEnt.ID] = newEnt;				
			return newEnt.ID;
		}
				
		//返回指定ID的实体
		public static function GetEntityByID(_id:int):Entity
		{
			return EntityIndexes[_id];
		}
		
	}
}